package com.zy.teacher.controller;


import com.zy.commont.result.Result;
import com.zy.teacher.entity.EduSubject;
import com.zy.teacher.entity.vo.OneSubject;
import com.zy.teacher.entity.vo.TwoSubject;
import com.zy.teacher.service.EduSubjectService;
import com.zy.teacher.service.EduSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author guli
 * @since 2019-08-20
 */
@RestController
@RequestMapping("/subject")
@CrossOrigin
public class EduSubjectController {

    @Autowired
    private EduSubjectService subjectService;

    /*把excel表数据导入到mysql数据库中去*/
    @PostMapping("import")
    public Result importSubject(MultipartFile file) {
        // 因为考虑到EXCL模板中数据不准确所以返回多个错误信息，那么多个错误信息放在集合中
        List<String> mesList = subjectService.importEXCL(file);
        if (mesList.size() == 0) {
            return Result.ok();
        } else {
            return Result.error().data("messageList", mesList);
        }
    }

    //    查询左侧课程的列表树
    @GetMapping("tree")
        public Result TreeSubject() {
        List<OneSubject> subjectList = subjectService.getTree();
        return Result.ok().data("subjectList", subjectList);
    }

    //  删除课程
    @DeleteMapping("{id}")
    public Result Delete(@PathVariable(value = "id") String id) {
        subjectService.deleteById(id);
        return Result.ok();
    }


    //  保存一级分类
    @PostMapping("saveLevelOne")
    public Result saveLevelOne(@RequestBody EduSubject eduSubject) {
        boolean save = subjectService.saveLevelOne(eduSubject);
        if (save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //  保存二级分类
    @PostMapping("saveLevelTwo")
    public Result saveLevelTwo(@RequestBody EduSubject eduSubject) {
        Boolean flag = this.subjectService.saveLevelTwo(eduSubject);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}

